package com.yanqu.road.dao.impl.cross.unionwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarSimple;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionWarSimpleDaoImpl extends TempDataObjectDao<CrossUnionWarSimple> {


    public Map<Integer, List<CrossUnionWarSimple>> getCrossUnionWarSimple(List<Integer> activityIdList) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<CrossUnionWarSimple>> dataMap = new ConcurrentHashMap<>();
        if(null == activityIdList || activityIdList.size() <= 0){
            return dataMap;
        }
        String getCrossUnionWarSimpleSql = "select * from t_c_activity_unionwar_simple where activityId in(" + StringUtils.listToString(activityIdList, ",") + ") ";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossUnionWarSimpleSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUnionWarSimple data = parse(rs);
                if(!dataMap.containsKey(data.getActivityId())){
                    dataMap.put(data.getActivityId(), new ArrayList<>());
                }
                dataMap.get(data.getActivityId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


    public static String insertCrossUnionWarSimpleSql = "insert into t_c_activity_unionwar_simple(activityId,unionUid,score1,score2,unionBaseInfo,modifyTime,no,groupId,commander) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossUnionWarSimple data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
//        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore1()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore2()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionBaseInfoJsonStr()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getModifyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNo()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCommander()));
        if (getDb().executeNoneQuery(insertCrossUnionWarSimpleSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossUnionWarSimpleSql = "update t_c_activity_unionwar_simple set score1=?,score2=?,unionBaseInfo=?,modifyTime=?,no=?,groupId=?,commander=? where activityId=? and unionUid=?";

    @Override
    protected boolean updateData(CrossUnionWarSimple data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore1()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore2()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionBaseInfoJsonStr()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getModifyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNo()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCommander()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
//        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        if (getDb().executeNoneQuery(updateCrossUnionWarSimpleSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossUnionWarSimple parseData(ResultSet rs) throws SQLException {
        CrossUnionWarSimple data = new CrossUnionWarSimple();
        data.setActivityId(rs.getInt("activityId"));
        data.setUnionUid(rs.getString("unionUid"));
//        data.setServerId(rs.getLong("serverId"));
        data.setScore1(rs.getInt("score1"));
        data.setScore2(rs.getInt("score2"));

        String baseUnionInfoStr = rs.getString("unionBaseInfo");
        if(!StringUtils.isNullOrEmpty(baseUnionInfoStr)){
            UnionBaseInfo unionBaseInfo = JSON.parseObject(baseUnionInfoStr, new TypeReference<UnionBaseInfo>() {});
            if(null != unionBaseInfo) {
                data.setUnionBaseInfo(unionBaseInfo);
            }
        }
        data.setModifyTime(rs.getLong("modifyTime"));
        data.setNo(rs.getInt("no"));
        data.setGroupId(rs.getInt("groupId"));
        data.setCommander(rs.getLong("commander"));
        return data;
    }
}
